<template>
    <div>
        <el-dialog
            :title="!form.id ? '新增' : '修改'"
            :visible.sync="visible"
            :close-on-click-modal="false">

            <div class="table_form_wrap">
                <el-form ref="form" :model="form" :rules="rules" @keyup.enter.native="handleSubmit()" label-width="120px">
                    <el-form-item label="父级职务" prop="pid">
                        <div class="cascader_wrap">
                            <div class="menu_cascader">
                                <MenuCascader
                                    ref="menuCascader"
                                    :disabledId.sync="disabledId"
                                    :cascaderMenuVal.sync="cascaderMenuVal"
                                    :checkStrictly="true"
                                    :clearable="false">
                                </MenuCascader>
                            </div>
                            <div class="delete">
                                <el-button type="primary" @click="clearVal">删除</el-button>
                            </div>
                        </div>
                    </el-form-item>
                    <el-form-item label="名称" prop="name">
                        <el-input placeholder="" v-model="form.name" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="路径" prop="url">
                        <el-input placeholder="" v-model="form.url" clearable style="width: 80%"></el-input>
                    </el-form-item>
                    <el-form-item label="排序" prop="sort">
                        <el-input-number v-model="form.sort" :min="1" style="width: 80%" placeholder="排序"></el-input-number>
                    </el-form-item>
                    <el-form-item label="状态" prop="status">
                        <el-radio-group v-model="form.status">
                            <el-radio-button :label="1">正常</el-radio-button>
                            <el-radio-button :label="0">锁定</el-radio-button>
                        </el-radio-group>
                    </el-form-item>
                </el-form>
            </div>


            <div slot="footer" class="dialog-footer">
                <el-button @click="visible = false">取 消</el-button>
                <el-button @click="handleSubmit()" type="primary" :submitLoading="submitLoading">提 交</el-button>
            </div>

        </el-dialog>
    </div>
</template>

<script>
import MenuCascader from '@/views/system/sysmenu/components/edit-menu-cascader'
export default {
    components: {
        MenuCascader
    },
    data() {
        return {
            visible: false,
            submitLoading: false,

            subName: null,

            disabledId: null,

            form: {
                pid: null,
                name: null,
                url: null,
                sort: 99,
                status: 1,
                ctime: null,
                utime: null
            },
            rules: {
                name: [{required: true, message: '名称不能为空', trigger: 'blur'}],
            },

            cascaderMenuVal: [],
        }
    },

    watch: {
        cascaderMenuVal(val) {
            //监听到路径变化需要查出这个三级分类的分组信息
            this.form.pid = val[val.length - 1];
        }
    },

    methods: {
        addRoot() {
            const refs = this.$refs
            this.form.id = null
            this.form.pid = 0
            this.form.level = 1
            this.visible = true

            this.cascaderMenuVal = []

            this.$nextTick(() => {
                refs['form'].resetFields()
            })
        },

        append(data) {
            const refs = this.$refs
            this.form.id = null
            this.form.pid = data.id;
            this.form.level = 1;
            this.visible = true

            this.$nextTick(() => {
                refs['form'].resetFields()
            })
        },

        edit(data) {
            const refs = this.$refs
            this.visible = true

            this.cascaderMenuVal = []
            this.disabledId = null

            this.$nextTick(() => {
                refs['form'].resetFields()
                if(data.id) {
                    this.disabledId = data.id
                    this.getInfo(data.id)
                }
            })
        },

        /**
         * 详细信息
         */
        async getInfo(id) {
            if (id) {
                const res = await this.$http({
                    url: '/system/menu/info/' + id,
                    method: 'GET'
                })
                this.form = Object.assign({}, res.data)

                setTimeout(() => {
                    // 去除当前节点，只留父节点
                    this.cascaderMenuVal = this.form.cascaderMenuPathVals.slice(0, this.form.cascaderMenuPathVals.length-1)
                }, 500);
            }
        },

        /**
         * 提交表单
         */
        async handleSubmit() {
            const refs = this.$refs
            this.submitLoading = true
            const valid = await refs['form'].validate().catch(() => {})
            if(valid) {
                const res = await this.$http({
                    url: `/system/menu/${!this.form.id ? 'save' : 'update'}`,
                    method: 'POST',
                    data: this.form
                })
                if (this.ResultMessage(res)) {
                    this.$emit('refreshDataList')
                    this.submitLoading = false
                    this.visible = false
                } else {
                    this.submitLoading = false
                }
            } else {
                this.submitLoading = false
            }
        },

        /**
         * 清空数据，重置PID为0
         */
        clearVal() {
            this.form.pid = 0
            this.$refs.menuCascader.clearVal()
        }

    }
};
</script>

<style lang="scss" scoped>
.cascader_wrap{
    display: flex;
    justify-content: flex-start;
    width: 80%;
    .menu_cascader{
        width: 100%;
    }
    .delete {
        display: flex;
        margin-left: 10px;
    }
}
</style>
